package com.magic233.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.magic233.entity.Message;
import com.magic233.entity.Orders;
import com.magic233.entity.Users;
import com.magic233.mapper.MessageMapper;
import com.magic233.timingTask.TimingTask;
import com.magic233.utils.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: MessageServiceImpl
 * Description:
 * date: 2021/6/5 13:14
 * author: zouyuan
 */
@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessageMapper messageMapper;

    @Value("${weixin.template_id1}")
    private String template_id1; //维修进度通知模板

    @Value("${weixin.template_id2}")
    private String template_id2; //维修完成微信通知模板

    private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    @Override
    public int sendMessage(Long senderid, Long recipientid, String content) {
        Long id = new Date().getTime();
        String time = simpleDateFormat.format(id);
        Message message = new Message();
        message.setId(id);
        message.setSenderid(senderid);
        message.setRecipientid(recipientid);
        message.setContent(content);
        message.setStatus("未读");
        message.setTime(time);

        int re = messageMapper.insert(message);
        return re;
    }


    @Override
    public Map<Object, Object> selectMessage(Message message, Integer pageNum, Integer pageSize) {
        System.out.println(pageNum);
        System.out.println(pageSize);
        if (pageNum != null && pageSize != null) {
            System.out.println("设置分页");
            PageHelper.startPage(pageNum, pageSize);
        }

        Page<Message> messages = messageMapper.selectMessage(message);
        long total = messages.getTotal();
        Map<Object, Object> map = new HashMap<>();
        map.put("messages", messages);
        map.put("total", total);
        return map;
    }

    @Override
    public int updateMessageStatus(Users user, Long id) {
        Message message = new Message();
        message.setId(id);
        message.setRecipientid(user.getId());
        message.setStatus("已读");
        int re = messageMapper.updateStatus(message);
        return re;
    }

    @Override
    //维修进度通知
    public int sendWeixinMessage1(Users user1, Users users2, Orders order) {
        //推送消息
        String sendMessageURL = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + TimingTask.accessToken;

        String data = "{\n" +
                "    \"touser\": \"" + user1.getOpenid() + "\",\n" +
                "    \"template_id\": \"" + template_id1 + "\",\n" +
                "    \"data\": {\n" +
                "        \"character_string1\": {\n" +
                "            \"value\": \"" + order.getId() + "\"\n" +
                "        },\n" +
                "        \"thing2\": {\n" +
                "            \"value\": \"" + order.getReason() + "\"\n" +
                "        },\n" +
                "        \"thing3\": {\n" +
                "            \"value\": \"" + order.getStatus() + "\"\n" +
                "        },\n" +
                "        \"thing5\": {\n" +
                "            \"value\": \"" + users2.getUsername() + "\"\n" +
                "        },\n" +
                "        \"time4\": {\n" +
                "            \"value\": \"" + order.getUpdatetime() + "\"\n" +
                "        }\n" +
                "    }\n" +
                "}";

        System.out.println(data);

        String s = null;
        try {
            s = HttpUtil.doPost(sendMessageURL, data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(s);
        return 0;
    }

    @Override
    //工人维修完成
    public int sendWeixinMessage2(Users user1, Users users2, Orders order) {
        //推送消息
        String sendMessageURL = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + TimingTask.accessToken;

        String data = "{\n" +
                "    \"touser\": \"" + user1.getOpenid() + "\",\n" +
                "    \"template_id\": \"" + template_id2 + "\",\n" +
                "    \"page\": \"" + "pages/userPage/order/maintainOrders/maintainOrders?noLogin=true" + "\",\n" +
                "    \"data\": {\n" +
                "        \"date3\": {\n" +
                "            \"value\": \"" + order.getStarttime() + "\"\n" +
                "        },\n" +
                "        \"thing7\": {\n" +
                "            \"value\": \"" + order.getAddress() + "\"\n" +
                "        },\n" +
                "        \"thing14\": {\n" +
                "            \"value\": \"" + order.getReason() + "\"\n" +
                "        },\n" +
                "        \"name9\": {\n" +
                "            \"value\": \"" + users2.getUsername() + "\"\n" +
                "        },\n" +
                "        \"phone_number13\": {\n" +
                "            \"value\": \"" + users2.getPhone() + "\"\n" +
                "        }\n" +
                "    }\n" +
                "}";

        System.out.println(data);

        String s = null;
        try {
            s = HttpUtil.doPost(sendMessageURL, data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(s);
        return 0;
    }
}
